﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace Studio77.AbbLinkLink.Core
{
    public class StageManager
    {
        public static StageManager Instance = new StageManager();
        private static string StageKey = "stageInfoKey";
        private static string PlaySoundKey = "playSoundKey";

        ApplicationDataContainer localSettings;

        /// <summary>
        /// key-游戏类型  value-当前关卡
        /// </summary>
        public Dictionary<int, int> StageInfo = new Dictionary<int, int>();

        public bool isPlaySound = true;

        private StageManager()
        {

        }

        public void Init(ApplicationDataContainer localSettings)
        {
            this.localSettings = localSettings;
            if (localSettings.Values.ContainsKey(StageKey))
            {
                try
                {
                    string stageStr = (string)localSettings.Values[StageKey];
                    string[] list = stageStr.Split(',');
                    foreach (string item in list)
                    {
                        string[] values = item.Split(':');
                        StageInfo[Convert.ToInt32(values[0])] = Convert.ToInt32(values[1]);
                    }
                }
                catch (Exception ex)
                {
                    StageInfo[1] = 0;
                    StageInfo[2] = 0;
                    StageInfo[3] = 0;
                }
            }
            else
            {

                //初始化三个难度
                StageInfo[1] = 0;
                StageInfo[2] = 0;
                StageInfo[3] = 0;

                SaveStage();
            }

            if (localSettings.Values.ContainsKey(PlaySoundKey))
            {
                isPlaySound = (bool)localSettings.Values[PlaySoundKey];
            }
        }

        public void SavePlaySoundState()
        {
            localSettings.Values[PlaySoundKey] = isPlaySound;
        }

        public void SaveStage()
        {
            string str = string.Empty;
            for (int i = 1; i <= 3; i++)
            {
                str += i.ToString() + ":" + StageInfo[i].ToString() + ",";
            }

            localSettings.Values[StageKey] = str.Substring(0, str.Length - 1);
        }
    }
}
